﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace DynamicGeometry
{
    [StyleFor(typeof(IShapeWithInterior))]
    public class ShapeStyle : LineStyle
    {
        public override FrameworkElement GetSampleGlyph()
        {
            var polygon = Factory.CreatePolygonShape();
            polygon.Points = new PointCollection() 
            {
                new Point(0, 20),
                new Point(10, 0),
                new Point(20, 20)
            };
            polygon.Apply(this.GetWpfStyle());
            polygon.Tag = this;
            return polygon;
        }

        Color mFill = Colors.Gray;
        [PropertyGridVisible]
        public Color Fill
        {
            get
            {
                return mFill;
            }
            set
            {
                mFill = value;
                OnPropertyChanged("Fill");
            }
        }

        Brush mFillBrush = null;
        [PropertyGridVisible]
        public Brush FillBrush
        {
            get
            {
                if (mFillBrush == null)
                    return new SolidColorBrush(Fill);
                return mFillBrush;
            }
            set
            {
                mFillBrush = value;
                OnPropertyChanged("FillBrush");
            }
        }

        protected override void ApplyToWpfStyle(Style existingStyle, IFigure figure)
        {
            base.ApplyToWpfStyle(existingStyle, figure);
            var brush = FillBrush;
            if (figure != null && figure.Selected && !(this is PointStyle))
            {
                brush.Opacity = 0.2;
            }
            var fillSetter = new Setter(Shape.FillProperty, brush);
            existingStyle.Setters.Add(fillSetter);
        }

        public override string ToString()
        {
            return base.ToString() + " " + Fill.ToString();
        }
    }
}